package org.apache.osgimaker.analyse.algorithm.util;

/**
 * Logical NOT operation on the result of a wrapped {@link StringPattern}.
 * 
 */
public class NotStringPattern implements StringPattern
{
  private final StringPattern _pattern;
  
  /**
   * Creates an instance based on the specified pattern.
   * @param pattern Pattern who's matching result will be negate.
   *        Must be not <tt>null</tt>.
   */
  public NotStringPattern(StringPattern pattern)
  {
    _pattern = pattern;
  }

  /**
   * Returns <tt>true</tt> if the wrapped {@link StringPattern} returns
   * <tt>false</tt> and vice-versa.
   */
  public boolean matches(String string)
  {
    return !_pattern.matches(string);
  }

  public String toString()
  {
    String expression = _pattern.toString();
    boolean bracketsNeeded = expression.startsWith("(") == false && expression.indexOf(' ') > 0;
    return '!' + (bracketsNeeded ? '(' + expression + ')' : expression);
  }
}
